<include file="public/header" title="新增收货地址" body="pore_add"/>
<include file="public/header_nav" title="新增收货地址" href="javascript:history.back(-1)"/>
<div class="floor my p edit">
    <form action="{:U('Mobile/User/add_address')}" method="post" id="addressForm">
        <div class="content">
            <div class="floor list7">
                <div class="myorder p">
                    <div class="content30">
                        <a href="javascript:void(0)">
                            <div class="order">
                                <div class="fl">
                                    <span>收&nbsp;货 &nbsp;人:</span>
                                </div>
                                <div class="fl">
                                    <input type="text" value="{$address.consignee}" name="consignee" placeholder="输入收货人" />
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="myorder p">
                    <div class="content30">
                        <a href="javascript:void(0)">
                            <div class="order">
                                <div class="fl">
                                    <span>手机号码:</span>
                                </div>
                                <div class="fl">
       <input type="tel" value="{$address.mobile}" placeholder="输入手机号码" name="mobile" onkeyup="this.value=this.value.replace(/[^\d]/g,'')"/>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="myorder p">
                    <div class="content30">
                        <a href="javascript:void(0)" onclick="locationaddress(this);">
                            <div class="order">
                                <div class="fl">
                                    <span>所在地区: </span>
                                </div>
                                <div class="fl">
                                    <input id="area" readonly="readonly" placeholder="选择地址" value=""  type="text">
                                    <input type="hidden" value="" id="province" name="province" class="hiddle_area"/>
                                    <input type="hidden" value="" id="province_name" class="hiddle_area"/>
                                    <input type="hidden" value="" id="city" name="city" class="hiddle_area"/>
                                    <input type="hidden" value="" id="city_name" class="hiddle_area"/>
                                    <input type="hidden" value="" id="district" name="district" class="hiddle_area"/>
                                    <input type="hidden" value="" id="district_name" class="hiddle_area"/>
                                </div>
                                <div class="fr">
                                    <i class="Mright"></i>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="myorder p">
                    <div class="content30">
                        <a href="javascript:void(0)">
                            <div class="order">
                                <div class="fl">
                                    <span>详细地址:</span>
                                </div>
                                <div class="fl">
                                    <input type="text" placeholder="输入详细地址" value="{$address.address}" name="address"/>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="myorder p">
                    <div class="content30">
                        <a href="javascript:void(0)">
                            <div class="order">
                                <div class="fl">
                                    <span>设为默认地址</span>
                                </div>
                                <div class="fr">
                                    <i id='default_addr' class="Mright turnoff <if condition="$status eq 1">turnup</if>"></i>
                                </div>
                            </div>
                            <input type="hidden" name="is_default" value="{$address.is_default}"/>
                    </div>
                    </a>
                </div>
            </div>
        </div>
        <input type="hidden" name="id" value="{$address.address_id}" />
        <div class="edita">
            <div class="content30">
                <input type="button" value="选择微信地址" class="dotm_btn1 beett" style="background-color: #4aca4a;" onclick="openAddress()" />
                <if condition="$Request.param.source eq 'cart2' "> <!--如果是下订单时提交过了的页面-->
                    <input type="button" value="保存并使用该地址" class="dotm_btn1 beett" onclick="checkForm()" />
                    <input type="hidden" name="source" value="{$Request.param.source}" />
                    <input type="hidden" name="order_id" value="{$Request.param.order_id}" />
                    <input type="hidden" name="act_id" value="{$Request.param.act_id}" />
                    <input type="hidden" name="goods_num" value="{$Request.param.goods_num}" />
                    <elseif condition="$Request.param.source eq 'sjvip' " />
                    <input type="button" value="保存该地址" class="dotm_btn1 beett" onclick="checkForm()" />
                    <input type="hidden" name="source" value="{$Request.param.source}" />
                    <input type="hidden" name="taocan_id" value="{$Request.param.taocan_id}" />
                    <else/>
                    <input type="button" value="保存该地址" class="dotm_btn1 beett" onclick="checkForm()" />
                    <input type="hidden" name="source" value="{$Request.param.source}" />
                </if>
            </div>
        </div>
    </form>
</div>
<!--选择地区-s-->
<div class="container" >
    <div class="city">
        <div class="screen_wi_loc">
            <div class="classreturn loginsignup">
                <div class="content">
                    <div class="ds-in-bl return seac_retu">
                        <a href="javascript:void(0);" onclick="closelocation();"><img src="__STATIC__/images/return.png" alt="返回"></a>
                    </div>
                    <div class="ds-in-bl search center">
                        <span class="sx_jsxz">选择地区</span>
                    </div>
                    <div class="ds-in-bl suce_ok">
                        <a href="javascript:void(0);">&nbsp;</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="province-list"></div>
        <div class="city-list" style="display:none"></div>
        <div class="area-list" style="display:none"></div>
    </div>
</div>
<!--选择地区-e-->
<div class="ed_shdele">
    <div class="sfk">是否删除该地址?</div>
    <div class="lineq">
        <span class="clos">取消</span>
        <span class="sur">确定</span>
    </div>
</div>
<div class="mask-filter-div" style="display: none;"></div>
<script src="__STATIC__/js/mobile-location.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

<script type="text/javascript">
    $(function(){
        $('.turnoff').click(function(){
            $(this).toggleClass('turnup');
            $("input[name=is_default]").val(Number($(this).hasClass('turnup')));
        });

        $.ajax({
            type : "POST",
            url:"/index.php?m=Mobile&c=Index&a=ajaxGetWxConfig&t="+Math.random(),
            data:{'askUrl':encodeURIComponent(location.href.split('#')[0])},        
            dataType:'JSON',
            success: function(res)
            {
                //微信配置
                wx.config({
                    debug: false, 
                    appId: res.appId,
                    timestamp: res.timestamp, 
                    nonceStr: res.nonceStr, 
                    signature: res.signature,
                    jsApiList: ['openAddress'] // 功能列表，我们要使用JS-SDK的什么功能
                });
            },
            error:function(){
                return false;
            }
        });
    })

    function openAddress(){
        wx.ready(function(){
            wx.openAddress({
                success: function (res) {
                    var userName = res.userName; // 收货人姓名
                    var postalCode = res.postalCode; // 邮编
                    var provinceName = res.provinceName; // 国标收货地址第一级地址（省）
                    var cityName = res.cityName; // 国标收货地址第二级地址（市）
                    var countryName = res.countryName; // 国标收货地址第三级地址（国家）
                    var detailInfo = res.detailInfo; // 详细收货地址信息
                    var nationalCode = res.nationalCode; // 收货地址国家码
                    var telNumber = res.telNumber; // 收货人手机号码
                    address(userName, telNumber, provinceName+cityName+countryName, detailInfo);
                    getAddressCode(provinceName, cityName, countryName);
                }
            });
        });
    }

    function address(userName, telNumber, area, detailInfo){
        $("input[name=consignee]").val(userName);
        $("input[name=mobile]").val(telNumber);
        $("#area").val(area);
        $("input[name=address]").val(detailInfo);
    }

    function getAddressCode(provinceName, cityName, countryName){
        $.ajax({
            type : "POST",
            url:"/index.php?m=Mobile&c=User&a=getAddressCode",
            data:{'provinceName':provinceName,'cityName':cityName,'countryName':countryName},        
            dataType:'JSON',
            success: function(res)
            {
                var msg = res.msg;
                $("input[name=province]").val(msg.provinceid);
                $("input[name=city]").val(msg.cityid);
                $("input[name=district]").val(msg.districtid);
            },
            error:function(){
                return false;
            }
        });
    }
</script>
<script type="text/javascript">
    $(function(){
        $('.menu').click(function(){
            $('.ed_shdele').show();
            $('.mask-filter-div').show();
        })
        $('.ed_shdele .clos').click(function(){
            $('.ed_shdele').hide();
            $('.mask-filter-div').hide();
        })
    });
</script>
<script type="text/javascript">
    function checkForm(){
        var consignee = $('input[name="consignee"]').val();
        var address = $('input[name="address"]').val();
        var mobile = $('input[name="mobile"]').val();
        var area = $('#area').val();
        var error = '';
        if(consignee == ''){
            error += '收货人不能为空 <br/>';
        }
        if(address == ''){
            error += '请填写地址 <br/>';
        }
        if(!checkMobile(mobile)){
            error += '手机号码格式有误 <br/>';
        }
        if(area == '') {
            error += '所在地区不能为空 <br/>';
        }
        if(error){
            layer.open({content:error,time:2});
            return false;
        }

        return $('#addressForm').submit();
    }
</script>
<script type="text/javascript">
    function locationaddress(e){
        $('.container').animate({width: '14.4rem', opacity: 'show'}, 'normal',function(){
            $('.container').show();
        });
        if(!$('.container').is(":hidden")){
            $('body').css('overflow','hidden')
            cover();
            $('.mask-filter-div').css('z-index','9999');
        }
    }
    function closelocation(){
        var province_div = $('.province-list');
        var city_div = $('.city-list');
        var area_div = $('.area-list');
        if(area_div.is(":hidden") == false){
            area_div.hide();
            city_div.show();
            province_div.hide();
            return;
        }
        if(city_div.is(":hidden") == false){
            area_div.hide();
            city_div.hide();
            province_div.show();
            return;
        }
        if(province_div.is(":hidden") == false){
            area_div.hide();
            city_div.hide();
            $('.container').animate({width: '0', opacity: 'show'}, 'normal',function(){
                $('.container').hide();
            });
            undercover();
            $('.mask-filter-div').css('z-index','inherit');
            return;
        }
    }
    $('body').on('click', '.area-list p', function () {
        //var area = ' '+getCookie('province_name')+' '+getCookie('city_name')+' '+getCookie('district_name');
        var area = ' '+$("#province_name").val()+' '+$("#city_name").val()+' '+$("#district_name").val();
        $("#area").val(area);
         $('.mask-filter-div').css('display','none');
        // $("input[name=province]").val(getCookie('province_id'));
        // $("input[name=city]").val(getCookie('city_id'));
        // $("input[name=district]").val(getCookie('district_id'));
    });
</script>
</body>
</html>
